package com.lzb.compressmaster.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.DocumentsAdapter;
import com.lzb.compressmaster.basic.media.MediaInfo;
import com.lzb.compressmaster.basic.media.MediaLoader;
import com.lzb.compressmaster.databinding.ActivitySelDocumentBinding;
import com.lzb.compressmaster.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class SelDocumentActivity extends BaseAc<ActivitySelDocumentBinding> implements View.OnClickListener, OnItemClickListener {
    private DocumentsAdapter documentsAdapter;
    private List<String> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_document;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelDocumentBack.setOnClickListener(view -> getOnBackPressedDispatcher());

        mDataBinding.tvPackageCompress.setOnClickListener(this);

        mDataBinding.rvDocumentList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        documentsAdapter = new DocumentsAdapter();
        documentsAdapter.setEdit(true);
        mDataBinding.rvDocumentList.setAdapter(documentsAdapter);
        documentsAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvPackageCompress) {//压缩包格式压缩
            if (listSel.size() == 0) {
                ToastUtils.showShort(getString(R.string.please_sel_picture));
                return;
            }
            FileCompressActivity.listPath = listSel;
            startActivity(FileCompressActivity.class);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (documentsAdapter.getItem(position).isSelected()) {
            documentsAdapter.getItem(position).setSelected(false);
            listSel.remove(documentsAdapter.getItem(position).getPath());
        } else {
            documentsAdapter.getItem(position).setSelected(true);
            listSel.add(documentsAdapter.getItem(position).getPath());
        }
        documentsAdapter.notifyItemChanged(position);
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listSel.size()));
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getData();
    }

    private void getData() {
        //设置获取的数据类型
        List<String> type = new ArrayList<>();
        type.add("application/msword");
        type.add("application/vnd.ms-excel");
        type.add("application/vnd.ms-powerpoint");
        type.add("application/pdf");
        type.add("text/plain");
        RxUtil.create(new RxUtil.Callback<List<MediaInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MediaInfo>> emitter) {
                List<MediaInfo> infoList = MediaLoader.loadDoc(type);
                emitter.onNext(infoList);
            }

            @Override
            public void accept(List<MediaInfo> infoList) {
                if (infoList != null && infoList.size() != 0) {
                    documentsAdapter.setList(infoList);
                    mDataBinding.rvDocumentList.setVisibility(View.VISIBLE);
                    mDataBinding.flDocumentNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvDocumentList.setVisibility(View.GONE);
                    mDataBinding.flDocumentNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}